package at.ac.uniklu.rallyemaster.activities;

import java.text.NumberFormat;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import at.ac.uniklu.rallyemaster.R;
import at.ac.uniklu.rallyemaster.adapters.DBTrackingPoint;
import at.ac.uniklu.rallyemaster.domain.TrackingPoint;

public class ChangeInformationActivity extends Activity {
	
	static final private int SAVE_ITEM = Menu.FIRST; 
	static final private int OPTIONS_ITEM = Menu.FIRST +1;
	
	private DBTrackingPoint dbTrackingPoint;
	private TrackingPoint trackingPoint;

	//inits the fields for the informations
	private EditText location;
	private EditText avgSpeed;
	private EditText speed;
	private EditText distance;
	private EditText notes;
	
	// photo and video views
	//TODO insert
	
	private CheckBox isCurve;
	private RadioGroup curveDirection;
	private TextView directionView;
	private RadioButton leftCurve;
	private RadioButton rightCurve;
	private RatingBar ratingCurve;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit_information_layout);
		
		int pointID = getIntent().getExtras().getInt("PointID", -1);
		
		dbTrackingPoint = new DBTrackingPoint(); 
		trackingPoint = dbTrackingPoint.getTrackByID(pointID);
		
		NumberFormat nf = NumberFormat.getInstance();
		nf.setMaximumFractionDigits(4);
		
		location = (EditText) findViewById(R.id.location);
		location.setText(nf.format(trackingPoint.getLatitude()) + " / " + nf.format(trackingPoint.getLongitude()));
		
		avgSpeed = (EditText) findViewById(R.id.avgSpeed);
		avgSpeed.setText((int)trackingPoint.getAvgSpeed() + " km/h");
		
		speed = (EditText) findViewById(R.id.speed);
		speed.setText((int)trackingPoint.getCurrentSpeed() + " km/h");
		
		distance = (EditText) findViewById(R.id.distance);
		distance.setText(nf.format(trackingPoint.getDistance()) + " m");
		
		notes = (EditText) findViewById(R.id.note);
		notes.setText(trackingPoint.getNotes());
		
		isCurve = (CheckBox)findViewById(R.id.checkCurve);
		isCurve.setChecked(trackingPoint.getCurve());
		isCurve.setOnCheckedChangeListener(new OnCheckedChangeListener(){

			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if(isChecked){
					directionView.setVisibility(TextView.VISIBLE);
					curveDirection.setVisibility(RadioGroup.VISIBLE);
					ratingCurve.setVisibility(RatingBar.VISIBLE);
					
				}else{
					directionView.setVisibility(TextView.GONE);
					curveDirection.setVisibility(RadioGroup.GONE);
					ratingCurve.setVisibility(RatingBar.GONE);
				}
				
			}
			
		});
		
		
		ratingCurve =(RatingBar)findViewById(R.id.ratingcurve);
		curveDirection = (RadioGroup) findViewById(R.id.curveDirection);
		directionView = (TextView)findViewById(R.id.direction_view);
		
		if(isCurve.isChecked()){
			if(trackingPoint.getCurveDirection() == TrackingPoint.CURVE_LEFT){
				leftCurve= (RadioButton) findViewById(R.id.left);
				leftCurve.setEnabled(true);
			}else{
				rightCurve= (RadioButton) findViewById(R.id.right);
				rightCurve.setEnabled(true);
			}
			ratingCurve.setRating(trackingPoint.getCurveIntension());
		}else{
			directionView.setVisibility(TextView.GONE);
			curveDirection.setVisibility(RadioGroup.GONE);
			ratingCurve.setVisibility(RatingBar.GONE);
		}
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu){
		MenuItem editItem = menu.add(0, SAVE_ITEM, Menu.NONE, R.string.itemMenu_store);
		MenuItem optionsItem = menu.add(0, OPTIONS_ITEM, Menu.NONE, R.string.itemMenu_options);
		
		editItem.setIcon(android.R.drawable.ic_menu_save);
		optionsItem.setIcon(android.R.drawable.ic_menu_preferences);
		
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		super.onContextItemSelected(item);
		
		TrackingPoint tp = buildTrackingPoint();
		switch(item.getItemId()){
		case SAVE_ITEM:{
			dbTrackingPoint.updateTrackingPoint(tp);
			Toast toast = Toast.makeText(getApplicationContext(), R.string.notification_save, Toast.LENGTH_SHORT);
			toast.show();
			//Intent intent = new Intent(this, ShowTrackActivity.class);
			//intent.putExtra("TrackID", tp.getTrackId());
			//startActivity(intent);
			this.finish();
			return true;
		}
		case OPTIONS_ITEM:{
			Intent intent = new Intent(this, PreferencesActivity.class);
			startActivity(intent);
			return true;
		}
		}
		return false;
	}
	
	/**
	 * Builds a tracking point from the changed informations in the from
	 * @return a valid tracking point
	 */
	public TrackingPoint buildTrackingPoint(){
		TrackingPoint tp = new TrackingPoint(); 
		
		tp.setId(trackingPoint.getId());
		tp.setLatitude(trackingPoint.getLatitude());
		tp.setLongitude(trackingPoint.getLongitude());
		tp.setDistance(trackingPoint.getDistance());
		tp.setNotes(notes.getText().toString());
		tp.setAvgSpeed(trackingPoint.getAvgSpeed());
		tp.setCurrentSpeed(trackingPoint.getCurrentSpeed());
		tp.setTime(trackingPoint.getTime());
		tp.setTrackId(trackingPoint.getTrackId());
		tp.setCurve(isCurve.isChecked());
		if(isCurve.isChecked()){ // if a curve is checked then store the values of the curve
			if(curveDirection.getCheckedRadioButtonId()==0)
				tp.setCurveDirection(TrackingPoint.CURVE_LEFT);
			else
				tp.setCurveDirection(TrackingPoint.CURVE_RIGHT);
			tp.setCurveIntension((int)ratingCurve.getRating());
		}else{ //else set all values of the curve zero
			tp.setCurveDirection(0);
			tp.setCurveIntension(0);
		}
		return tp;
	}
	

}
